﻿using System;
using UnityEditor;
using UnityEngine;


namespace RStudio.UIFramwork.Editor.Utils
{
    /// <summary>
    /// 编辑器扩展
    /// </summary>
    public class EditUIComponentUtil
    {
        #region Toggle
        /// <summary>
        /// 扩展 Toggle
        /// </summary>
        /// <param name="label"></param>
        /// <param name="value"></param>
        /// <param name="target"></param>
        /// <param name="options"></param>
        public static void UnitToggle(string label, ref bool value, UnityEngine.Object target = null, Action onClick = null)
        {
            bool isSelected = GUILayout.Toggle(value, label, "button", GUILayout.MinWidth(80f), GUILayout.MinHeight(20f), GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
            if (isSelected)
            {
                onClick?.Invoke();
            }

            if (value.Equals(isSelected)) return;

            value = isSelected;

            if (target != null)
            {
                EditorUtility.SetDirty(target);
            }
        }
        #endregion

        #region InputField

        /// <summary>
        /// 扩展 InputField
        /// </summary>
        /// <param name="label"></param>
        /// <param name="text"></param>
        /// <param name="target"></param>
        /// <param name="options"></param>
        public static void UnitInputField(string label, ref string text, UnityEngine.Object target = null)
        {
            GUILayout.BeginHorizontal("box", GUILayout.ExpandWidth(false), GUILayout.Width(50f));
            {
                GUILayout.Label(label, GUILayout.MinWidth(30f), GUILayout.ExpandWidth(true));
                string input = GUILayout.TextField(text, GUILayout.ExpandWidth(true), GUILayout.MinWidth(50));

                if (input != text && target != null)
                {
                    EditorUtility.SetDirty(target);
                }
            }
            GUILayout.EndHorizontal();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <param name="value"></param>
        /// <param name="target"></param>
        public static void UnitInputField(string label, ref int value, UnityEngine.Object target = null)
        {
            string input = value.ToString();
            UnitInputField(label, ref input, target);

            if (input != value.ToString())
            {
                bool isOk = int.TryParse(input, out int newValue);
                if (isOk && newValue != value)
                {
                    value = newValue;

                    if (target != null)
                    {
                        EditorUtility.SetDirty(target);
                    }
                }
            }
        }

        #endregion

        #region Slider
        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <param name="value"></param>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <param name="target"></param>
        public static void UnitSlider(string label, ref float value, float minValue, float maxValue, UnityEngine.Object target = null)
        {
            EditorGUILayout.BeginHorizontal("box", GUILayout.ExpandWidth(false), GUILayout.Width(200f));
            {
                EditorGUILayout.LabelField(label, GUILayout.MinWidth(50f));
                float newValue = EditorGUILayout.Slider(value, minValue, maxValue, GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true));

                if (newValue != value)
                {
                    value = newValue;
                    EditorUtility.SetDirty(target);
                }
            }
            EditorGUILayout.EndHorizontal();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <param name="value"></param>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <param name="target"></param>
        public static void UnitSlider(string label, ref int value, int minValue, int maxValue, UnityEngine.Object target = null)
        {
            float newFloat = float.Parse(value.ToString());
            float newMax = float.Parse(maxValue.ToString());
            float newMin = float.Parse(minValue.ToString());

            UnitSlider(label, ref newFloat, newMin, newMax, target);

            int newInt = Mathf.FloorToInt(newFloat);
            if (newInt != value && target != null)
            {
                value = newInt;
                EditorUtility.SetDirty(target);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <param name="value"></param>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <param name="target"></param>
        public static void UnitSlider(string label, ref uint value, uint minValue, uint maxValue, UnityEngine.Object target = null)
        {
            float newFloat = float.Parse(value.ToString());
            float newMax = float.Parse(maxValue.ToString());
            float newMin = float.Parse(minValue.ToString());

            UnitSlider(label, ref newFloat, newMin, newMax, target);

            int newInt = Mathf.FloorToInt(newFloat);
            if (newInt != value && target != null)
            {
                value = (uint)newInt;
                EditorUtility.SetDirty(target);
            }
        }
        #endregion
    }
}
